package OSMS.action.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.CustomerDao;
import OSMS.entity.Customers;
public class Cus_Update_Msg_Servlet extends HttpServlet {
	CustomerDao cdao = new CustomerDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		Object obj_cus = session.getAttribute("cus");
		Customers cus = (Customers)obj_cus;
		int cid = cus.getCid();
		
		String 	username = request.getParameter("username");
		String 	sex = "";
				sex = request.getParameter("sex");
		String 	address = "";
				address = request.getParameter("address");
		System.out.println(username);
		System.out.println(sex);
		System.out.println(address);
		if(username.equals(""))
		{
			request.setAttribute("fail_code", 1);
			request.getRequestDispatcher("../user/fail_to_update_msg.jsp").forward(request, response);
			return ;
		}
		else if(!sex.equals("男")&&!sex.equals("女"))
		{
			request.setAttribute("fail_code", 2);
			request.getRequestDispatcher("../user/fail_to_update_msg.jsp").forward(request, response);
			return ;
		}
		else
		{
			boolean flag_udt_msg = false;
			try {
				flag_udt_msg = cdao.upDateCusMsg(username,sex,address,cid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Customers after_cus = new Customers();
			try {
				after_cus = cdao.getCusById(cus.getCid());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			session.setAttribute("cus", after_cus);
			request.getRequestDispatcher("../user/successful_to_update_msg.jsp").forward(request, response);
			
		}
				
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
